|
|
|
|
|
|
|
can define the array even if the objects don't yet exist. Unfortunately, all of the issues of array processing discussed earlier are still present when working with object arrays that you define. |
|
|
|
|
|
|
|
|
However, Visual Basic does support one type of object array that you will find yourself using a lot, even though collections are always available. When you copy a control and paste that control back onto the form, Visual Basic displays a message box informing you that you are about to create a control array (see Figure 5.2). |
|
|
|
|
|
|
|
|
Figure 5.2.
Pasting several of the same controls on a form will
create a control array. |
|
|
|
|
|
|
|
|
After you have a control array on the form, you can use it to manipulate all the elements. The events for a control array contain a parameter that passes the index of the current control to the event routine. Therefore, if you enter text into one of the five text boxes on a form, the txtInput_KeyPress routine would know which control you are typing into. As an example, if you wanted to create a keypress routine that would allow only numbers to be entered, you could code the following in the KeyPress event: |
|
|
|
|
|
|
|
|
Private Sub txtInputLine_KeyPress(Index As Integer, keyascii As Integer)
If (keyascii < 48 Or keyascii > 58) And keyascii <> 8 Then
Beep
keyascii = 0
End If
End Sub |
|
|
|
|
|
|
|
|
Of course, if this was the only thing that you needed to do in the routine, it would be better to use the Masked Edit control, which does this type of editing for you. To use this feature in a more understandable way, open the dlgFileCopyOpen project. In this application, when you allow the user to change the colors for the form and the controls, you must code each of the color statements separately for each control: |
|
|
|
|
|
|
|
|
frmDialog!txtSourceFile.BackColor = lblTxtBack.BackColor
frmDialog!txtTargetFile.BackColor = lblTxtBack.BackColor |
|
|
|
|
|
|
|
|
Note Visual Basic uses the exclamation character (!) instead of the standard period (.) when referencing a form's objects from within another form or module. |
|
|
|
|
|
|